package com.hucheng.reconciliation.biz;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-07-30
 * @Description: 支付宝下载账单
 **/
@Slf4j
@Component
public class ReconciliationFileDownBiz {

    /**
     * 下载支付宝账单
     *
     * @param urlStr
     * @param filePath
     * @return boolean
     */
    public boolean downLoadBiz(String urlStr, String filePath) {
        URL url = null;
        HttpURLConnection httpUrlConnection = null;
        InputStream fis = null;
        FileOutputStream fos = null;

        boolean flag = false;

        try {
            url = new URL(urlStr);
            httpUrlConnection = (HttpURLConnection) url.openConnection();
            httpUrlConnection.setConnectTimeout(5 * 1000);
            httpUrlConnection.setDoInput(true);
            httpUrlConnection.setDoOutput(true);
            httpUrlConnection.setUseCaches(false);
            httpUrlConnection.setRequestMethod("GET");
            httpUrlConnection.setRequestProperty("Charsert", "UTF-8");
            httpUrlConnection.connect();
            fis = httpUrlConnection.getInputStream();
            byte[] temp = new byte[1024];
            int b;
            fos = new FileOutputStream(new File(filePath));
            while ((b = fis.read(temp)) != -1) {
                fos.write(temp, 0, b);
                fos.flush();
            }
            flag = true;
            log.info("支付宝对账 >>>>>>>>>> 文件下载成功");
        } catch (MalformedURLException e) {
            log.info("支付宝对账 >>>>>>>>>> 文件下载失败，失败原因 " + e);
            e.printStackTrace();
        } catch (IOException e) {
            log.info("支付宝对账 >>>>>>>>>> 文件下载失败，失败原因 " + e);
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (httpUrlConnection != null) {
                    httpUrlConnection.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }

    /**
     * 通过支付宝查询对账单接口返回的url,下载zip文件
     *
     * @param alipay_url
     * @param down_url
     * @return
     */
    public boolean downLoadZip(String alipay_url, String down_url) {
        boolean down_success = false;
        int bytesum = 0;
        int byteread = 0;
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd");
        String dateFloder = sf.format(date);
        InputStream inStream = null;
        FileOutputStream fs = null;
        try {
            URL url = new URL(alipay_url);
            URLConnection conn = url.openConnection();
            inStream = conn.getInputStream();
            //自定义文件保存地址//判断下载保存路径文件夹
            String unzipFilePath = down_url.substring(0, down_url.lastIndexOf("\\"));
            //下载文件存放地址
            File unzipFileDir = new File(unzipFilePath);
            if (!unzipFileDir.exists() || !unzipFileDir.isDirectory()) {
                unzipFileDir.mkdirs();
            }
            //解压文件是否已存在
            File entryFile = new File(down_url);
            if (entryFile.exists()) {
                //删除已存在的目标文件
                entryFile.delete();
            }
            fs = new FileOutputStream(down_url);
            byte[] buffer = new byte[4028];
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                fs.write(buffer, 0, byteread);
            }
            down_success = true;
            log.info("支付宝对账 >>>>>>>>>> 文件下载成功");
        } catch (Exception e) {
            log.info("支付宝对账 >>>>>>>>>> 文件下载" + dateFloder + "失败，失败原因 " + e);
            return false;
        } finally {
            try {
                if (inStream != null) {
                    inStream.close();
                }
            } catch (IOException e) {
                inStream = null;
            }
            try {
                if (fs != null) {
                    fs.close();
                }
            } catch (IOException e) {
                fs = null;
            }
        }
        return down_success;
    }
}
